package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.Key;
import com.frank.libandroid.butterknife.OnClick;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.FestivalBean;
import com.migu.contacts.bean.FestivalTip;
import com.migu.contacts.bean.GiftBean;
import com.migu.contacts.bean.GiftTip;
import com.migu.contacts.databinding.ActivityFestivalBinding;
import com.migu.contacts.databinding.FestivalSelectPopBinding;
import com.migu.contacts.util.DateUtil;
import com.migu.contacts.util.PopUtil;
import com.migu.contacts.widget.Callback;
import com.migu.contacts.widget.Label2;

import java.time.LocalDate;
import java.util.List;

public class FestivalActivity extends BaseActivity<ActivityFestivalBinding> {

    private FestivalTip festivalTip;

    @Override
    protected void initData() {
        super.initData();
        festivalTip = DataStore.get(FestivalActivity.KEY);
        if (festivalTip == null) {
            festivalTip = new FestivalTip();
        }
        if (festivalTip.giftTip == null) {
            festivalTip.giftTip = new GiftTip();
        }
        if (TextUtils.isEmpty(festivalTip.tipTime)) {
            festivalTip.tipTime = DateUtil.getMothDay(LocalDate.now());
        }
        if (TextUtils.isEmpty(festivalTip.giftTip.tipTime)) {
            festivalTip.giftTip.tipTime = DateUtil.getMothDay(LocalDate.now());
        }
        mBinding.setBean(festivalTip);
        Label2.LabelBean festivalLabelBean = new Label2.LabelBean("选择提醒节日", festivalTip.festivalName, 0, true);
        mBinding.tip.setBean(festivalLabelBean);
        mBinding.tip.mBinding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                API.queryFestival().watch(mActivity, new HttpCallback<List<FestivalBean>>() {
                    @Override
                    protected void onSuccess(List<FestivalBean> groupBeans) {
                        showFestivalPicker(groupBeans);
                    }
                });

            }
        });
        Label2.LabelBean festivalDateLabel = new Label2.LabelBean("提醒日期", festivalTip.tipTime, 0, true);
        mBinding.dateTip.setBean(festivalDateLabel);
        mBinding.dateTip.mBinding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopUtil.showMonthDayPicker(mActivity, festivalDateLabel.right, new Callback<String>() {
                    @Override
                    public void onComplete(String target) {
                        festivalDateLabel.right = target;
                        festivalDateLabel.notifyChange();
                        festivalTip.tipTime = target;
                    }
                });
            }
        });
        Label2.LabelBean giftDateLabel = new Label2.LabelBean("提醒日期", festivalTip.giftTip.tipTime, 0, true);
        mBinding.giftTip.setBean(giftDateLabel);
        mBinding.giftTip.mBinding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopUtil.showMonthDayPicker(mActivity, giftDateLabel.right, new Callback<String>() {
                    @Override
                    public void onComplete(String target) {
                        giftDateLabel.right = target;
                        giftDateLabel.notifyChange();
                        festivalTip.giftTip.tipTime = target;
                    }
                });
            }
        });
    }

    private void showFestivalPicker(List<FestivalBean> groupBeans) {
        BottomPopup bottomPopup = new BottomPopup<FestivalSelectPopBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @Override
            protected void initView() {
                super.initView();
                mBinding.edu.setFormatter(new WheelFormatter() {
                    @Override
                    public String formatItem(@NonNull Object item) {
                        FestivalBean bean = (FestivalBean) item;
                        return bean.name;
                    }
                });
                mBinding.edu.setData(groupBeans);
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                int currentPosition = mBinding.edu.getCurrentPosition();
                List<FestivalBean> data = (List<FestivalBean>) mBinding.edu.getData();
                FestivalBean festivalBean = data.get(currentPosition);
                festivalTip.festivalName = festivalBean.name;
                festivalTip.festivalId = festivalBean.id;
                FestivalActivity.this.mBinding.tip.setBean(new Label2.LabelBean("选择提醒节日", festivalBean.name, 0, true));
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }


    public void save(View view) {
        DataStore.put(KEY, festivalTip);
        setResult(RESULT_OK);
        finish();
    }

    public static final Key<FestivalTip> KEY = new Key<FestivalTip>();

    public void goGift(View view) {
        startActivityForResult(GiftSelectActivity.class, new Runnable() {
            @Override
            public void run() {
                GiftBean giftBean = DataStore.get(GiftSelectActivity.KEY);
                if (giftBean == null) {
                    return;
                }
                FestivalTip birthdayTip = mBinding.getBean();
                if (birthdayTip.giftTip == null) {
                    birthdayTip.giftTip = new GiftTip();
                }
                birthdayTip.giftTip.content = giftBean.name;
            }
        });
    }

    public void goWords(View view) {
        start(FestivalWordsActivity.class);
    }
}